package cn.dakaqi.services.user.impl;

import cn.dakaqi.dao.user.VolunteerInsuranceDao;
import cn.dakaqi.entities.user.VolunteerInsurance;
import cn.dakaqi.services.user.VolunteerInsuranceService;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Created by chunyang on 2016/4/19.
 */
@Component(value = "volunteerInsuranceService")
@Transactional
public class VolunteerInsuranceServiceImpl implements VolunteerInsuranceService
{
    @Autowired
    VolunteerInsuranceDao volunteerInsuranceDao;

    @Override
    public void create(String param) throws ServiceRuntimeException
    {

    }

    @Override
    public VolunteerInsurance save(VolunteerInsurance volunteerInsurance) throws ServiceRuntimeException
    {
        return volunteerInsuranceDao.save(volunteerInsurance);
    }

    @Override
    public VolunteerInsurance findByVolunteerCode(String volunteerCode) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteerCode", new SearchFilter("volunteerCode", SearchFilter.Operator.EQ, volunteerCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, VolunteerInsurance.STATUS_ACTIVATION));
        Specification<VolunteerInsurance> spec = DynamicSpecifications.bySearchFilter(filters.values(), VolunteerInsurance.class);
        List<VolunteerInsurance> list = this.volunteerInsuranceDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public VolunteerInsurance findByVolunteerCodeAndStatus(String volunteerCode, int status) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("volunteerCode", new SearchFilter("volunteerCode", SearchFilter.Operator.EQ, volunteerCode));
        filters.put("status", new SearchFilter("status", SearchFilter.Operator.EQ, status));
        Specification<VolunteerInsurance> spec = DynamicSpecifications.bySearchFilter(filters.values(), VolunteerInsurance.class);
        List<VolunteerInsurance> list = this.volunteerInsuranceDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public void applyInsurance(String volunteerCode, String guardianName, String guardianIDCard, String guardianMobile)
    {

    }
}
